package com.wan.data.mysql.orm.convert.auto;

import com.wan.data.mysql.orm.convert.AutoConverter;
import com.wan.data.mysql.orm.meta.ColumnMeta;
import com.wan.data.mysql.util.DbColumnType;

import java.util.concurrent.atomic.AtomicBoolean;

public class BoolConverter implements AutoConverter {
    @Override
    public Class<?>[] convertType() {
        return new Class[]{boolean.class,Boolean.class, AtomicBoolean.class};
    }

    @Override
    public DbColumnType getDbType() {
        return DbColumnType.BOOLEAN;
    }

    @Override
    public Object dbToEntity(ColumnMeta columnMeta, Object value) throws Exception {
        if(columnMeta.getFieldType() == AtomicBoolean.class){
            return new AtomicBoolean((boolean) value);
        }
        return value;
    }

    @Override
    public Object entityToDb(ColumnMeta columnMeta, Object value) throws Exception {
        boolean bool;
        if(value instanceof AtomicBoolean){
            bool = ((AtomicBoolean) value).get();
        }else{
            bool = (boolean) value;
        }
        return bool? 1: 0;
    }
}
